package com.best.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtils {
	// 默认7天
	private static int keepTime = 604800;

	public static void setCookie(String name, String value, HttpServletResponse response) {
		Cookie cookie = new Cookie(name, value);
		cookie.setMaxAge(keepTime);
		cookie.setPath("/");
		response.addCookie(cookie);
	}

	public static String getCookie(String name, HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		String result = "";
		if (null != cookies) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals(name)) {
					result = cookie.getValue();
					break;
				}
			}
		}
		return result;
	}
	
	public static void removeCookie(String name,HttpServletResponse response){
		Cookie cookie = new Cookie(name, null);
		cookie.setPath("/");
		cookie.setMaxAge(0);
		response.addCookie(cookie);
	}

}
